package com.ymt.bpm.web.rest.mobile;

import com.alibaba.fastjson.JSONObject;
import com.ymt.bpm.service.mobile.MQueryService;
import com.ymt.bpm.util.Const;
import com.ymt.bpm.util.EncodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Johnny on 2017/10/10.
 */
@RestController
@RequestMapping("/mapi/v1/engine/taskquery")
public class MQueryResource {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MQueryService mqs;

    @GetMapping("/todos")
    public JSONObject todos(HttpServletRequest req,
                            @RequestParam(value="lang", defaultValue=Const.LANG_ZH)String lang,
                            @RequestParam(value="filter", required = false)String filter,
                            @RequestParam(value="order", defaultValue="DESC")String order,
                            @RequestParam(value="sort", defaultValue="T.CREATE_TIME")String sort,
                            @RequestParam("offset")int offset,
                            @RequestParam("limit")int limit,
                            @RequestParam("timezoneOffset")int timezoneOffset,
                            @RequestParam(value="bpdId", required = false)String bpdId,
                            @RequestParam(value="search", required = false)String search) {
        if (log.isDebugEnabled()) {
            log.debug("todos search=" + search);
        }
        search = EncodeUtil.decode(search);
        String tenantId = (String)req.getAttribute(Const.TENANT_ID);
        String userId = (String)req.getAttribute(Const.JWT_USER);
        if (log.isDebugEnabled()) {
            log.debug("todos tenantId=" + tenantId);
            log.debug("todos lang=" + lang);
            log.debug("todos userId=" + userId);
            log.debug("todos order=" + order);
            log.debug("todos sort=" + sort);
            log.debug("todos offset=" + offset);
            log.debug("todos limit=" + limit);
            log.debug("todos timezoneOffset=" + timezoneOffset);
            log.debug("todos bpdId=" + bpdId);
            log.debug("todos search=" + search);
        }
        return mqs.todos(tenantId, userId, bpdId, search, filter, order, sort, offset, limit, lang, timezoneOffset);
    }

    @GetMapping("/list/{queryName}")
    public JSONObject dones(HttpServletRequest req,
                            @PathVariable(value="queryName")String queryName,
                            @RequestParam(value="status")String status,
                            @RequestParam(value="lang", defaultValue=Const.LANG_ZH)String lang,
                            @RequestParam(value="order", required = false, defaultValue="DESC")String order,
                            @RequestParam(value="sort", required = false)String sort,
                            @RequestParam("offset")int offset,
                            @RequestParam("limit")int limit,
                            @RequestParam("timezoneOffset")int timezoneOffset,
                            @RequestParam(value="bpdId", required = false)String bpdId,
                            @RequestParam(value="search", required = false)String search) {
        if (log.isDebugEnabled()) {
            log.debug("list search=" + search);
        }
        search = EncodeUtil.decode(search);
        String tenantId = (String)req.getAttribute(Const.TENANT_ID);
        String userId = (String)req.getAttribute(Const.JWT_USER);
        if (log.isDebugEnabled()) {
            log.debug("list queryName=" + queryName);
            log.debug("list tenantId=" + tenantId);
            log.debug("list lang=" + lang);
            log.debug("list userId=" + userId);
            log.debug("list order=" + order);
            log.debug("list sort=" + sort);
            log.debug("list offset=" + offset);
            log.debug("list limit=" + limit);
            log.debug("list timezoneOffset=" + timezoneOffset);
            log.debug("list bpdId=" + bpdId);
            log.debug("list search=" + search);
        }
        return mqs.queryList(queryName, tenantId, status, userId, bpdId, search, order, sort, offset, limit, lang, timezoneOffset);
    }

}
